package com.sctu.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.EMSApp.R;
import com.sctu.EMSApp.ScenictourSurroundClickActivity;
import com.sctu.adapter.SurroundAdapter;
import com.sctu.bean.SurroundBean;
import com.sctu.bean.SurroundImageBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.List;


/**
 * Created by zhu on 2015/11/3.
 */
public class ScenictourSurroundFragment extends Fragment {

    private ImageView imageView;
    private View view;
    private ListView listView;
    private LinearLayout linearLayoutl;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_scenictour_surround, null);
        listView = (ListView) view.findViewById(R.id.fragment_scenicTour_round_listView);
        imageView = (ImageView) view.findViewById(R.id.fragment_scenicTour_round_image);
        linearLayoutl = (LinearLayout) view.findViewById(R.id.fragment_scenicTour_round_linear);

        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

        LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) linearLayoutl.getLayoutParams();
        linearParams.height = (int) (height*0.33);

        initheadimg();
        initlistview();
        return view;

    }

    private void initlistview() {


        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_SCENIC_SURROUND_LIST,
                listener, errorlistener);
    }

    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String jsonstring) {
            Log.e("Json", jsonstring);

            SurroundBean results = JSON.parseObject(jsonstring, SurroundBean.class);
            final List<SurroundBean.Results> rr = results.getResults();

            listView.setAdapter(new SurroundAdapter(rr, getActivity()));

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(),ScenictourSurroundClickActivity.class);
                    Log.i("id", String.valueOf(rr.get(position).getId()));
                    intent.putExtra("id",String.valueOf(rr.get(position).getId()));
                    startActivity(intent);
                }
            });
        }

    };

    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(getActivity(), "网络出了点小问题", Toast.LENGTH_LONG)
                    .show();

        }
    };


    private void initheadimg() {

        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_SCENIC_SURROUND_IMAGE,
                listenerhead, errorlistenerhead);
    }

    Response.Listener<String> listenerhead = new Response.Listener<String>() {

        @Override
        public void onResponse(String jsonstring) {
            Log.e("Json", jsonstring);
            SurroundImageBean results = JSON.parseObject(jsonstring, SurroundImageBean.class);
            List<SurroundImageBean.Results> rr = results.getResults();
            GetVolleyHttp.getInstance(getActivity()).setImageLoader(imageView, R.id.fragment_scenicTour_round_image, R.id.fragment_scenicTour_round_image, UrlConfig.URL + rr.get(0).getPath());
        }
    };

    Response.ErrorListener errorlistenerhead = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(getActivity(), "网络出了点问题", Toast.LENGTH_LONG)
                    .show();

        }
    };

}
